package com.gko100.dao.impl;

import java.util.List;

import com.gko100.dao.AdministratorDao;
import com.gko100.dao.basehibernatesupport.BaseHibernateDaoSupport;
import com.gko100.model.Administrator;

public class AdministratorDaoHibernate extends BaseHibernateDaoSupport implements AdministratorDao {

	
	public void delete(Administrator admin) {
		this.getHibernateTemplate().delete(admin);
		
	}

	
	public void delete(Integer adminId) {
		
		this.getHibernateTemplate().delete(this.findAdminByAdminId(adminId));
		
	}

	public List<Administrator> findAdminAll() {
		
		List<Administrator> adminList = this.getHibernateTemplate().find("from Administrator");
		if(adminList!=null&&adminList.size()>=1){
			return adminList;
		}else{
			logger.debug(this.getClass().getMethods().toString()+"为空");
			return null;
		}
	}
	
	public List<Administrator> findAdminAll(int pageNo, int pageSize) {
		
		if(pageNo<1){
			return null;
		}
		int offset=(pageNo-1)*pageSize;
		List<Administrator> adminList = this.findByPage("from Administrator", offset, pageSize);
		if(adminList!=null&&adminList.size()>=1){
			return adminList;
		}else{
			logger.debug(this.getClass().getMethods().toString()+"为空");
			return null;
		}
	}

	 
	public Administrator findAdminByName(String name) {
		
		List<Administrator> admins = this.getHibernateTemplate().find("from Administrator as a where a.name=?",name);
		if(admins!=null&&admins.size()>=1){
			return admins.get(0);
		}else{
			logger.debug(this.getClass().getMethods().toString()+"为空");
			return null;
		}
		
		
	}

	
	public Administrator findAdminByNameAndPass(String name, String password) {
		
		List<Administrator> admins=this.getHibernateTemplate()
		.find("from Administrator as a where a.name=? and a.password=?",name, password);
		if(admins!=null&&admins.size()>=1){
			return admins.get(0);
		}else{
			logger.debug(this.getClass().getMethods().toString()+"为空");
			return null;
		}
	}

	
	public Administrator findAdminByAdminId(Integer adminId) {
		
		Administrator admin = this.getHibernateTemplate().get(Administrator.class, adminId);
		if(admin!=null){
			return admin;
		}else{
			logger.debug(this.getClass().getMethods().toString()+"为空");
			return null;
		}
	}

	
	public void save(Administrator admin) {
		
		this.getHibernateTemplate().save(admin);
	}

	
	public void update(Administrator admin) {
		
		this.getHibernateTemplate().update(admin);
	}

	public int getAdminCount(){
		
		if(this.findAdminAll()!=null){
			return this.findAdminAll().size();
		}else{
			return 0;
		}
		
	}
}
